package org.example;

import com.sun.org.apache.bcel.internal.ExceptionConstants;
import org.apache.maven.surefire.shade.org.apache.maven.shared.utils.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Set;

import static java.lang.Thread.sleep;

public class Main {
    public static void main(String[] args) throws InterruptedException, IOException {
        //test01();
        //test02();
        //test03();
        //test04();
        //test05();
        //test06();
        //test07();
        //test08();
        //test09();
        test10();
    }

    /**
     * 浏览器截图
     */
    private static void test10() throws InterruptedException, IOException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("双11");
        //点击
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        //截图
        File file = ((TakesScreenshot)webDriver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(file,new File("D://1wppppp//jietu.png"));
    }

    /**
     * 关闭浏览器
     * 获取窗口句柄
     */
    private static void test09() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
//        webDriver.findElement(By.cssSelector("#kw")).sendKeys("双11");
//        webDriver.findElement(By.cssSelector("#su")).click();
        webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();
        sleep(3000);
        //获取所有页面页面的句柄
        Set<String> windowHandles = webDriver.getWindowHandles();
        String targ = "";
        for (String handle : windowHandles){
            targ = handle;
        }
        //切换句柄
        webDriver.switchTo().window(targ);
        webDriver.findElement(By.cssSelector("#ww")).sendKeys("计算机就业");
        sleep(3000);
        webDriver.findElement(By.cssSelector("#s_btn_wr")).click();
        sleep(3000);
        //直接关闭浏览期并清除缓存
//        webDriver.quit();
        //关闭上一级跳转页面，不会清除缓存
         webDriver.close();
    }

    /**
     * 鼠标点击
     */
    private static void test08() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("双11");
        //点击
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        WebElement webElement = webDriver.findElement(By.cssSelector("#s_tab > div > a.s-tab-item.s-tab-item_1CwH-.s-tab-pic_p4Uej.s-tab-pic"));
        Actions actions = new Actions(webDriver);
        sleep(3000);
        actions.moveToElement(webElement).contextClick().perform();

    }

    /**
     * 键盘输入
     * @throws InterruptedException
     */
    private static void test07() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        //搜索
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("双11");
        //点击
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL,"A");
        sleep(3000);
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL,"x");
        sleep(3000);
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL,"v");
        sleep(3000);
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL,"c");
    }

    /**
     * 浏览器前进后退操作
     */
    private static void test06() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        //搜索
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("双11");
        //点击
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        //浏览器后退
        webDriver.navigate().back();
        sleep(3000);
        //刷新操作
        webDriver.navigate().refresh();
        sleep(3000);
        //浏览器前进
        webDriver.navigate().forward();
        sleep(3000);
        //浏览器滚动条操作
        ((JavascriptExecutor)webDriver).executeScript("document.documentElement.scrollTop=10000");
        sleep(3000);
        //浏览器最大化
        webDriver.manage().window().maximize();
        sleep(3000);
        //全屏
        webDriver.manage().window().fullscreen();
        sleep(3000);
        //设置大小
        webDriver.manage().window().setSize(new Dimension(600,1000));
    }

    /**
     * 显示等待只在规定时间内等待页面
     * 隐式等待等待页面加载完成才找元素
     */
    private static void test05() {
//        ChromeOptions options = new ChromeOptions();
//        options.addArguments("--remote-allow-origins=*");

        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        //判断元素是否可以被点击
        //隐式等待
        WebDriverWait wait = new WebDriverWait(webDriver,1);
        wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#bottom_layer > div > p:nth-child(9)")));
    }

    private static void test04() {
        ChromeOptions chromeOptions = new ChromeOptions();
        //允许所有请求
        chromeOptions.addArguments("--remote-allow-origins=*");
        //实例化浏览器
        WebDriver webDriver = new ChromeDriver(chromeOptions);
        //操控浏览器去打开百度网站
        webDriver.get("https://www.baidu.com");
        //获取元素中的对应的属性值
        String text = webDriver.findElement(By.cssSelector("#su")).getAttribute("value");
        if(text.contains("百度一下")){
            System.out.println("测试通过");
        }else {
            System.out.println("测试不通过");
        }
    }

    private static void test03() {
        ChromeOptions chromeOptions = new ChromeOptions();
        //允许所有请求
        chromeOptions.addArguments("--remote-allow-origins=*");
        //实例化浏览器
        WebDriver webDriver = new ChromeDriver(chromeOptions);
        //操控浏览器去打开百度网站
        webDriver.get("https://www.baidu.com");
        //通过xpath,找到页面元素
       webDriver.findElement(By.xpath("//a[text() = \"新闻\"]")).click();
    }

    /**
     * 清空操作
     */
    private static void test02() {
        ChromeOptions chromeOptions = new ChromeOptions();
        //允许所有请求
        chromeOptions.addArguments("--remote-allow-origins=*");
        //实例化浏览器
        WebDriver webDriver = new ChromeDriver(chromeOptions);
        //操控浏览器去打开百度网站
        webDriver.get("https://www.baidu.com");
        //通过css类选择器,找到百度搜索输入框
        WebElement element = webDriver.findElement(By.cssSelector("#kw"));
        //通过xpath选择器
        //WebElement element = webDriver.findElement(By.xpath("//*[@id=\"kw\"]"));
        //往输入框输入信息
        element.sendKeys("牛客网");
        //点击
        webDriver.findElement(By.cssSelector("#su")).click();
        //清空输入框中的数据
        webDriver.findElement(By.cssSelector("#kw")).clear();
    }

    /**
     * 点击操作
     * @throws
     */
    private static void test01() throws InterruptedException {
        ChromeOptions chromeOptions = new ChromeOptions();
        //允许所有请求
        chromeOptions.addArguments("--remote-allow-origins=*");
        //实例化浏览器
        WebDriver webDriver = new ChromeDriver(chromeOptions);
        //操控浏览器去打开百度网站
        webDriver.get("https://www.baidu.com");
        //通过css类选择器,找到百度搜索输入框
        //WebElement element = webDriver.findElement(By.cssSelector(".s_ipt"));
        //通过xpath选择器
        WebElement element = webDriver.findElement(By.xpath("//*[@id=\"kw\"]"));
        //往输入框输入信息
        element.sendKeys("软件测试");
        //找到百度一下按钮
        //点击
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        //校验
        List<WebElement> elem = webDriver.findElements(By.cssSelector("a em"));
        for (int i = 0; i < elem.size(); i++) {
            System.out.println(elem.get(i).getText());
            //如果返回的结果有软件测试,证明测试通过,否则不通过
            if(elem.get(i).getText().contains("测试")){
                System.out.println("测试通过");
                return;
            }else {
                System.out.println("测试不通过");
                return;
            }
        }


    }
}